﻿/* 2011/11/25 */
using System;
using System.Collections.Specialized;
using System.Windows.Forms;
using PictureManager.ClassifyFile;

namespace PictureManager
{
    public partial class FileNameCategoryControl : UserControl
    {
        public FileNameCategoryControl()
        {
            InitializeComponent();

            foreach (OptionInformation aOption in FileNameCategory.AvailableOption)
                fileNameCategoryOptionComboBox.Items.Add(new CorrespondOptionItem(aOption.Option, aOption.Description));
        }

        public void LoadSettings(string fileCorrespondedRootFolder, StringCollection fileCorrespondenceRules)
        {
            fileNameCategoryRootFolderTextBox.Text = fileCorrespondedRootFolder;
            fileNameCategoryRuleListView.Items.Clear();
            foreach (string aRule in fileCorrespondenceRules)
            {
                CorrespondenceRuleItem theItem = new CorrespondenceRuleItem(aRule);
                fileNameCategoryRuleListView.Items.Add(theItem);
            }
        }

        public void SaveSettings(out string fileCorrespondedRootFolder, StringCollection fileCorrespondenceRules)
        {
            fileCorrespondedRootFolder = fileNameCategoryRootFolderTextBox.Text;
            fileCorrespondenceRules.Clear();
            ListView.ListViewItemCollection ruleItems = fileNameCategoryRuleListView.Items;
            foreach (ListViewItem aItem in ruleItems)
            {
                CorrespondenceRuleItem theItem = (CorrespondenceRuleItem)aItem;
                fileCorrespondenceRules.Add(theItem.EscapeString);
            }
        }

        private void fileNameCategoryAddButton_Click(object sender, EventArgs e)
        {
            CorrespondOptionItem theOptionItem = (CorrespondOptionItem)fileNameCategoryOptionComboBox.SelectedItem;
            if (theOptionItem == null)
            {
                MessageBox.Show("请选择匹配方式。", "添加规则失败");
                return;
            }
            string fileNamePattern = fileNameCategoryPatternTextBox.Text;
            if (String.IsNullOrEmpty(fileNamePattern))
            {
                MessageBox.Show("请输入匹配字符串。", "添加规则失败");
                return;
            }
            string folderPath = fileNameCategoryCorrespondedFolderTextBox.Text;
            if (String.IsNullOrEmpty(folderPath))
            {
                MessageBox.Show("请输入分类文件夹。", "添加规则失败");
                return;
            }
            CorrespondenceRuleItem item = new CorrespondenceRuleItem(fileNamePattern, folderPath, fileNameCategorySubFolderCheckBox.Checked, theOptionItem.Option);
            fileNameCategoryRuleListView.Items.Add(item);
            foreach (ListViewItem anItem in fileNameCategoryRuleListView.SelectedItems)
            {
                anItem.Selected = false;
            }
            item.Selected = true;
            int theIndex = fileNameCategoryRuleListView.Items.IndexOf(item);
            fileNameCategoryRuleListView.EnsureVisible(theIndex);
        }

        private void fileNameCategoryRemoveButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = fileNameCategoryRuleListView.SelectedItems;
            if (items.Count != 0)
            {
                foreach (ListViewItem aItem in items)
                {
                    fileNameCategoryRuleListView.Items.Remove(aItem);
                }
            }
        }

        private void fileNameCategoryModifyButton_Click(object sender, EventArgs e)
        {
            if (fileNameCategoryRuleListView.SelectedItems.Count != 1)
            {
                MessageBox.Show("请选择一个规则进行修改。", "修改失败");
                return;
            }

            CorrespondOptionItem theOptionItem = (CorrespondOptionItem)fileNameCategoryOptionComboBox.SelectedItem;
            if (theOptionItem == null)
            {
                MessageBox.Show("请选择匹配方式。", "修改失败");
                return;
            }
            string fileNamePattern = fileNameCategoryPatternTextBox.Text;
            if (String.IsNullOrEmpty(fileNamePattern))
            {
                MessageBox.Show("请输入匹配字符串。", "修改失败");
                return;
            }
            string folderPath = fileNameCategoryCorrespondedFolderTextBox.Text;
            if (String.IsNullOrEmpty(folderPath))
            {
                MessageBox.Show("请输入分类文件夹。", "修改失败");
                return;
            }

            CorrespondenceRuleItem theItem = (CorrespondenceRuleItem)fileNameCategoryRuleListView.SelectedItems[0];
            theItem.CorrespondOption = theOptionItem.Option;
            theItem.FileNamePattern = fileNamePattern;
            theItem.FolderPath = folderPath;
            theItem.IsSubFolder = fileNameCategorySubFolderCheckBox.Checked;
        }

        private void fileNameCategoryRuleListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            CorrespondenceRuleItem theItem = (CorrespondenceRuleItem)e.Item;
            if (e.IsSelected)
            {
                for (int i = 0; i < fileNameCategoryOptionComboBox.Items.Count; i++)
                {
                    if (((CorrespondOptionItem)fileNameCategoryOptionComboBox.Items[i]).Option == theItem.CorrespondOption)
                        fileNameCategoryOptionComboBox.SelectedIndex = i;
                }
                fileNameCategoryPatternTextBox.Text = theItem.FileNamePattern;
                fileNameCategoryCorrespondedFolderTextBox.Text = theItem.FolderPath;
                fileNameCategorySubFolderCheckBox.Checked = theItem.IsSubFolder;
            }
            else
            {
                if (fileNameCategoryRuleListView.SelectedItems.Count == 0)
                {
                    fileNameCategoryOptionComboBox.SelectedItem = null;
                    fileNameCategoryPatternTextBox.Text = String.Empty;
                    fileNameCategoryCorrespondedFolderTextBox.Text = String.Empty;
                    fileNameCategorySubFolderCheckBox.Checked = false;
                }
            }
        }

        private void fileNameCategoryRuleListView_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            CorrespondenceRuleItemSorter sorter = (CorrespondenceRuleItemSorter)fileNameCategoryRuleListView.ListViewItemSorter;
            switch (e.Column)
            {
                case 0:
                    break;
                case 1:
                    // Sort by pattern.
                    if (sorter == null || sorter.Option != CorrespondenceRuleItemSorter.SortOption.Pattern)
                    {
                        fileNameCategoryRuleListView.ListViewItemSorter = new CorrespondenceRuleItemSorter(
                            CorrespondenceRuleItemSorter.SortOption.Pattern);
                    }
                    else
                    {
                        fileNameCategoryRuleListView.ListViewItemSorter = null;
                        sorter.ReverseOrder();
                        fileNameCategoryRuleListView.ListViewItemSorter = sorter;
                    }
                    break;
                case 2:
                    // Sort by folder aPath.
                    if (sorter == null || sorter.Option != CorrespondenceRuleItemSorter.SortOption.Folder)
                    {
                        fileNameCategoryRuleListView.ListViewItemSorter = new CorrespondenceRuleItemSorter(
                            CorrespondenceRuleItemSorter.SortOption.Folder);
                    }
                    else
                    {
                        fileNameCategoryRuleListView.ListViewItemSorter = null;
                        sorter.ReverseOrder();
                        fileNameCategoryRuleListView.ListViewItemSorter = sorter;
                    }
                    break;

                default:
                    break;
            }
        }
    }
}
